package com.d3works.apps.project.service;

import com.d3works.common.Inflector;
import com.d3works.common.StringUtils;
import com.d3works.meta.db.MetaJsonField;

public class MetaJsonFieldProjectServiceImpl implements MetaJsonFieldProjectService {

  @Override
  public String computeSelectName(MetaJsonField jsonField, String typeSelect) {
    String selection = jsonField.getSelection();
    if (StringUtils.isEmpty(jsonField.getName())
        || !("select".equals(typeSelect) || "multiselect".equals(typeSelect))
        || StringUtils.notEmpty(selection)) {
      return selection;
    }

    Inflector inflector = Inflector.getInstance();
    String model =
        inflector
            .dasherize(jsonField.getModel().substring(jsonField.getModel().lastIndexOf('.') + 1))
            .replace("-", ".");
    String fieldName = inflector.dasherize(jsonField.getName()).replace("-", ".");

    return String.format("project.%s.json.field.%s.type.select", model, fieldName);
  }
}
